package ynu.edu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ynu.edu.po.OrderDetailet;
import ynu.edu.po.Orders;
import ynu.edu.service.IOrderDetailetService;
import ynu.edu.service.IOrdersService;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Tag(name = "订单服务")
public class OrdersController {
    @Resource
    private IOrdersService ordersService;

    @Resource
    private IOrderDetailetService orderDetailetService;

    @PostMapping("/createOrders")
    @Operation(summary = "创建订单",
            description = "根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录" +
                    "并获取自动生成的订单编号" +
                    "然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中" +
                    "然后根据用户编号、商家编号删除购物车表中的数据。")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "businessId", description = "商家编号"),
            @Parameter(name = "daId", description = "送货地址编号"),
            @Parameter(name = "orderTotal", description = "订单总金额")
    })
    public int createOrders(@RequestParam String userId,
                            @RequestParam Integer businessId,
                            @RequestParam Integer daId,
                            @RequestParam Double orderTotal) {
        Orders orders = Orders.builder()
                .userId(userId)
                .businessId(businessId)
                .daId(daId)
                .orderTotal(orderTotal)
                .build();
        return ordersService.createOrders(orders);
    }

    @PostMapping("/getOrdersById")
    @Operation(summary = "查询订单信息",
            description = "根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息")
    @Parameter(name = "orderId", description = "订单编号")
    public Orders getOrdersById(@RequestParam Integer orderId) {
        return ordersService.getOrdersById(orderId);
    }

    @PostMapping("/listOrdersByUserId")
    @Operation(summary = "查询订单信息",
            description = "根据用户编号查询此用户的所有订单信息")
    @Parameter(name = "userId", description = "用户编号")
    public List<Orders> listOrdersByUserId(@RequestParam String userId) {
        return ordersService.listOrdersByUserId(userId);
    }

    @PostMapping("/listOrderDetailetByOrderId")
    @Operation(summary = "查询订单详情",description = "根据订单编号查询此用户的所有订单详情信息")
    public List<OrderDetailet> listOrderDetailetByOrderId(Integer orderId){
        return orderDetailetService.listOrderDetailetByOrderId(orderId);
    }


    @PostMapping("/updateOrderStatus")
    @Operation(summary = "修改订单状态",description = "修改订单状态")
    public Boolean updateOrderStatus(@RequestParam("orderStatus") Integer orderStatus,@RequestParam("id") Integer id){
        return orderDetailetService.updateOrderStatus(orderStatus,id);
    }

}
